﻿using System.Collections.Generic;
using Entitas;
using System.Linq;
class PlayerMovementSystem : ReactiveSystem<CommandEntity>, ICleanupSystem
{
    CommandContext commandContext;
    PlayerContext playerContext;
    public PlayerMovementSystem(Contexts contexts) : base(contexts.command)
    {
        commandContext = contexts.command;
        playerContext = contexts.player;
    }

    protected override void Execute(List<CommandEntity> entities)
    {
        if (playerContext.count == 1)
        {
            var Player = playerContext.GetEntities().SingleEntity<PlayerEntity>();
            var NewPosition = Player.position.value;
            foreach (var e in entities)
            {
                switch (e.movementCommand.direction)
                {
                    case Direction.Up:
                        NewPosition = Player.position.value;
                        NewPosition.y += Player.speed.value;
                        Player.ReplacePosition(NewPosition);
                        break;
                    case Direction.Up_Right:
                        NewPosition = Player.position.value;
                        NewPosition.y += Player.speed.value;
                        NewPosition.x += Player.speed.value;
                        Player.ReplacePosition(NewPosition);
                        break;
                    case Direction.Up_Left:
                        NewPosition = Player.position.value;
                        NewPosition.y += Player.speed.value;
                        NewPosition.x -= Player.speed.value;
                        Player.ReplacePosition(NewPosition);
                        break;
                    case Direction.Down:
                        NewPosition = Player.position.value;
                        NewPosition.y -= Player.speed.value;
                        Player.ReplacePosition(NewPosition);
                        break;
                    case Direction.Down_Left:
                        NewPosition = Player.position.value;
                        NewPosition.y -= Player.speed.value;
                        NewPosition.x -= Player.speed.value;
                        Player.ReplacePosition(NewPosition);
                        break;
                    case Direction.Down_Right:
                        NewPosition = Player.position.value;
                        NewPosition.y -= Player.speed.value;
                        NewPosition.x += Player.speed.value;
                        Player.ReplacePosition(NewPosition);
                        break;
                    case Direction.Left:
                        NewPosition = Player.position.value;
                        NewPosition.x -= Player.speed.value;
                        Player.ReplacePosition(NewPosition);
                        break;
                    case Direction.Right:
                        NewPosition = Player.position.value;
                        NewPosition.x += Player.speed.value;
                        Player.ReplacePosition(NewPosition);
                        break;

                    default:
                        break;
                }
            }
        }
    }

    protected override bool Filter(CommandEntity entity)
    {
        if (playerContext.count == 1)
        {
            var player = playerContext.GetEntities().SingleEntity<PlayerEntity>();
            if (entity.hasMovementCommand)
            {
                if (entity.movementCommand.Owner == player.name.value)
                    return true;
                else return false;
            }
            else return false;
        }
        else return false;
    }

    protected override ICollector<CommandEntity> GetTrigger(IContext<CommandEntity> context)
    {
        return context.CreateCollector(CommandMatcher.MovementCommand.Added());
    }
    public void Cleanup()
    {
        if (playerContext.count == 1)
        {
            var player = playerContext.GetEntities().SingleEntity<PlayerEntity>();
            foreach (var e in commandContext.GetEntities().Where(x => x.hasMovementCommand && x.movementCommand.Owner == player.name.value))
            {
                e.Destroy();
            }
        }

    }
}
